﻿using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Marbles.Core.Settings;

namespace Marbles.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            if (SettingsManager.DeveloperMode) { return; }

            Exception ex = Server.GetLastError();

            if (ex.GetType() == typeof(HttpException))
            {
                if (ex.Message.Contains("NoCatch"))
                    return;

                Server.Transfer("~/Pages/HttpErrorPage.aspx");
            }
            Response.Clear();
            Response.ContentType = "text/html";
            Response.Write("<html><head><title>Runtime Error</title></head><body style=\"font-family:verdana,arial,helvetica,sans;font-size:11px;\">");
            Response.Write("<h2>Runtime Error</h2>\n");
            Response.Write("A Runtime Error has occured on the system. The System Administration team has been notified of this error.<br><br>");
            Response.Write("<b>" + ex.Message + "</b><br/><br/>\n");

            if (SettingsManager.ShowFullDebugInfoToClient)
            {
                Response.Write("<div style=\"border:solid 1px silver;background:#eeeeee;padding:2px;\"><pre>" + ex.ToString() + "</pre></div><br/><br/>\n");
            }

            Response.Write("<a href='/'>Click here to go home</a>");
            Response.Write("</body></html>");

            if (SettingsManager.EnableRunTimeErrorMailing)
            {
                SmtpClient c = Marbles.Core.Settings.MailSettings.CreateSmtpClient();
                MailMessage msg = new MailMessage();
                msg.Subject = string.Format("Runtime Error on {0}", Request.Url.ToString());
                msg.IsBodyHtml = true;
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.AppendLine("<html><head><title>Runtime Error</title></head><body style=\"font-family:verdana,arial,helvetica,sans;font-size:11px;\">");
                sb.AppendLine("<h2>Runtime Error</h2>\n");
                sb.AppendLine("<b>" + ex.Message + "</b><br/><br/>\n");
                sb.AppendLine("<div style=\"border:solid 1px silver;background:#eeeeee;padding:2px;\"><pre>" + ex.ToString() + "</pre></div><br/><br/>\n");
                sb.Append("<b>Request Details</b><br/><br/>");
                sb.AppendLine("<div style=\"border:solid 1px silver;background:#eeeeee;padding:2px;\">");
                sb.AppendLine("<b>Request Url : </b>" + Request.Url.ToString() + "<br>");
                sb.AppendLine("<b>Request Date : </b>" + DateTime.Now.ToString("ddd dd MMMM yyyy HH:mm:ss") + "<br>");
                sb.AppendLine("<b>Request Host : </b>" + (string.IsNullOrEmpty(Request.UserHostName) ? Request.UserHostAddress : Request.UserHostName) + "<br>");
                sb.AppendLine("<b>Request Agent : </b>" + Request.UserAgent + "<br>");
                sb.Append("</div><br><br><br>");
                sb.AppendLine("</body></html>");
                msg.Body = sb.ToString();
                msg.Priority = MailPriority.High;

                foreach (string s in SettingsManager.RunTimeErrorMailAddress.Split(','))
                {
                    if (string.IsNullOrEmpty(s.Trim())) { continue; }
                    if (msg.From == null) { msg.From = new MailAddress(s.Trim()); }
                    msg.To.Add(new MailAddress(s.Trim()));
                }

                if (msg.To.Count > 0) { c.Send(msg); }
            }

            Server.ClearError();
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}